﻿using System;
using System.Data;
using TIS.DAO;

namespace TIS.Administrator
{
    public partial class Home : System.Web.UI.Page
    {
        LanguageDAO _languageDao = new LanguageDAO();
        NewsDAO _newsDao = new NewsDAO();
        UserInfo _userInfo = new UserInfo();
        CategoryDAO _categoryDao = new CategoryDAO();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["user"] != null)
                {
                    if (Session["langId"] == null)
                    {
                        Session["langId"] = 1;
                    }
                    GetDataDdlLanguage();
                    BinDataHienThi();
                    LoadForm();
                }
                else
                {
                    Response.Redirect("Login.aspx");
                }
            }
        }


        public void GetDataDdlLanguage()
        {
            ddlLanguage.DataSource = _languageDao.GetAllLanguage();
            ddlLanguage.DataTextField = "LanguageName";
            ddlLanguage.DataValueField = "LanguageId";
            ddlLanguage.SelectedValue = Session["langId"].ToString();
            ddlLanguage.DataBind();
        }

        public void LoadForm()
        {
            if (Request.QueryString["id"] != null)
            {
                string id = Request.QueryString["id"];
                DataTable dt = _categoryDao.GetCategoryById(int.Parse(id));
                foreach (DataRow row in dt.Rows)
                {
                    txtCategoryName.Text = row["CategoryName"].ToString();
                    txtTitle.Text = row["CategoryTitle"].ToString();
                    txtDescription.Text = row["CategoryDescription"].ToString();
                    txtMetaKeyword.Text = row["CategoryKeyword"].ToString();
                    txtSEOUrl.Text = row["CategorySEOUrl"].ToString();
                    ddlDisplay.SelectedValue = row["CategoryDisplayCenter"].ToString();
                    txtPosition.Text = row["CategoryPosition"].ToString();
                    ddlLanguage.SelectedValue = row["LanguageId"].ToString();
                    if (row["CategoryParent"].ToString().Equals("1"))
                    {
                        cbParentDisplay.Checked = true;
                        cbParentHide.Checked = false;
                    }
                    else
                    {
                        cbParentDisplay.Checked = false;
                        cbParentHide.Checked = true;
                    }
                    if (row["CategoryStatus"].ToString().Equals("1"))
                    {
                        cbDisplay.Checked = true;
                        cbHide.Checked = false;
                    }
                    else
                    {
                        cbDisplay.Checked = false;
                        cbHide.Checked = true;
                    }
                }
            }
        }

        public void BinDataHienThi()
        {
            DataTable dtRole = new DataTable();

            DataColumn cell = new DataColumn();
            cell.ColumnName = "DisplayId";
            cell.DataType = Type.GetType("System.String");
            dtRole.Columns.Add(cell);

            cell = new DataColumn();
            cell.ColumnName = "Value";
            cell.DataType = Type.GetType("System.Int32");
            dtRole.Columns.Add(cell);

            DataRow row = dtRole.NewRow();
            row["DisplayId"] = "Top đầu trang";
            row["Value"] = "1";
            dtRole.Rows.Add(row);

            row = dtRole.NewRow();
            row["DisplayId"] = "Giữa trang";
            row["Value"] = "2";
            dtRole.Rows.Add(row);



            ddlDisplay.DataSource = dtRole;
            ddlDisplay.DataTextField = "DisplayId";
            ddlDisplay.DataValueField = "Value";
            ddlDisplay.DataBind();
        }


        protected void btnAddnew_Click(object sender, EventArgs e)
        {

            string categoryName = txtCategoryName.Text;
            string title = txtTitle.Text == "" ? categoryName : txtTitle.Text;
            string des = txtDescription.Text;
            string position = txtPosition.Text;
            string meteKeyword = txtMetaKeyword.Text == "" ? title : txtMetaKeyword.Text;
            string seoUrl = txtSEOUrl.Text == "" ? URLDecode.ConvertToUnSign(title) : txtSEOUrl.Text;
            int status;
            string displaycenter = ddlDisplay.SelectedValue;

            string imgSmall = "";
            int langId = int.Parse(ddlLanguage.SelectedValue);
            if (cbDisplay.Checked)
            {
                status = 1;
            }
            else
            {
                status = 0;
            }

            int parentId;
            if (cbParentDisplay.Checked)
            {
                parentId = 1;
            }
            else
            {
                parentId = 0;
            }
            if (Request.QueryString["id"] == null)
            {
                if (fileUpload.FileName != "")
                {
                    imgSmall = "/Upload/Image/" + fileUpload.FileName;
                    fileUpload.SaveAs(Server.MapPath("~/Upload/Image/") + fileUpload.FileName);
                }

                _categoryDao.InsertCategory(categoryName, parentId, title, des, meteKeyword, seoUrl, int.Parse(displaycenter),
                                            int.Parse(position), langId, status, imgSmall);

                Response.Redirect("ManagerCat.aspx");
            }
            else
            {
                string id = Request.QueryString["id"].ToString();
                if (fileUpload.FileName != "")
                {
                    imgSmall = "/Upload/Image/" + fileUpload.FileName;
                    fileUpload.SaveAs(Server.MapPath("~/Upload/Image/") + fileUpload.FileName);
                }
                _categoryDao.UpdateCategory(categoryName, parentId, title, des, meteKeyword, seoUrl, int.Parse(displaycenter), int.Parse(position), langId, status, imgSmall, int.Parse(id));
                Response.Redirect("ManagerCat.aspx");
            }
        }


    }
}